package com.lh.controller;

import com.lh.entity.UsianUser;
import com.lh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("user")
public class UserController {

    //登陆注册、忘记密码
    @Autowired
    UserService us;

    @RequestMapping("login")
    public String login(UsianUser user, HttpServletRequest request) {


        /**
         *
         * UsianUser userl = us.login(user);
         if (userl!=null){

         //登录成功 重定向展示页面
         return "redirect:/goods/toshow";
         }else{
         //登陆失败 重定向 登陆页面
         return "redirect:tologin";

         }

         */

        //返回布尔值--判断真假


        if (us.login(user)) {
            request.getSession().setAttribute("user", user);
            return "redirect:/goods/toshow";
        } else {//登陆失败
            return "redirect:tologin?errMsg=1";


        }


    }

    @RequestMapping("tologin")
    public String tologin(String errMsg, HttpServletRequest request) {

        if ("1".equals(errMsg)) {
            request.setAttribute("errMsg", "用户名密码错误");
        }

        return "login";

    }

    @RequestMapping("toregist")
    public String toregist() {


        return "regist";

    }

    @RequestMapping("toupdateUser")
    public String toupdateUser() {


        return "updateUser";

    }

    @RequestMapping("updateUser")
    public String updateUser(UsianUser user) {

        us.updateUser(user);

        return "redirect:tologin";

    }

    @RequestMapping("regist")
    public String regist(UsianUser user) {

        us.regist(user);

        return "redirect:tologin";

    }

}
